%{
#include "y.tab.h"
#include "tree.h"
#include <string.h>
#include <stdio.h>
 
extern int scannerError;
extern int lineno;
int commentdepth;
extern int fileno();
%}
%s comment
%s one_line_comment
%option noyywrap noinput nounput
%%
<INITIAL>{
[ \t]+        /* ignore */;
\n              lineno++;
"(*"			{commentdepth= 1;;BEGIN(comment);}
"#"				{BEGIN(one_line_comment);}

"func"			return tFUNC;
"end"			return tEND;
"int"			return tINT;
"bool"			return tBOOL;
"array of"		return tARRAY;
"record of"		return tRECORD;
"type"			return tTYPE;
"var"			return tVAR;
"return"		return tRETURN;
"write"			return tWRITE;
"allocate"		return tALLOCATE;
"if"			return tIF;
"then"			return tTHEN;
"while"			return tWHILE;
"do"			return tDO;
"of length"		return tOFLENGTH;
"else"			return tELSE;
"true"			return tTRUE;
"false"			return tFALSE;
"null"			return tNULL;
"=="			return tEQ;
"!="			return tNEQ;
">="			return tGEQ;
"<="			return tLEQ;
"&&"			return tAND;
"||"			return tOR;
"*"				return '*';
"/"				return '/';
"+"				return '+';
"-"				return '-';
"("				return '(';
")"				return ')';
"{"				return '{';
"}"				return '}';
"="				return '=';
","				return ',';
";"				return ';';
":"				return ':';
"."				return '.';
"<"				return '<';
">"				return '>';
"["				return '[';
"]"				return ']';
"|"				return '|';
"!"				return '!';

0|([1-9][0-9]*)        { yylval.intconst = atoi(yytext);
                       return tNUM; }
[a-zA-Z_][a-zA-Z0-9_]* { yylval.stringconst = (char *)malloc(strlen(yytext)+1);
                         sprintf(yylval.stringconst,"%s",yytext); 
                         return tID; }
.						{return 1;}
}
<one_line_comment>\n	{lineno++; BEGIN(INITIAL);}
<one_line_comment>.		/* ignore */;

<comment>"(*" 			{commentdepth++;}
<comment>"*)" 			{commentdepth--;if(commentdepth==0){ BEGIN(INITIAL);}}
<comment>\n				{lineno++;}
<comment><<EOF>>		{ fprintf(stderr,"Error:%d: Unclosed comment at EOF",lineno); scannerError++;yyterminate();}	
<comment>.				/* ignore */ ;
%%